<?php

namespace app\admin\model;

use think\Model;


class SmsRechargeLogModel extends Model
{

    

    

    // 表名
    protected $name = 'sms_recharge_log';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;


    // 追加属性
    protected $append = [

    ];




    public static function isCanSend($user_id)
    {
        return self::get([
            'user_id' => $user_id,
            'status' => 1,
            'able_num' => ['>', self::raw('send_num')]
        ]);
    }

    // 获取剩余短信条数
    public static function getSurplusNum($user_id)
    {
        return self::where([
            'user_id' => $user_id,
            'status' => 1,
        ])->sum('able_num - send_num');
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
